package com.example.demo.widget.chart.utils

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF

/**
 * 绘制文字工具类
 *
 * @author wangheng
 */
object DrawTextUtils {
    fun drawTextOnRectCenter(
        canvas: Canvas,
        paint: Paint,
        text: String,
        drawLeft: Float,
        drawTop: Float,
        drawRight: Float,
        drawBottom: Float) {
        val fontMetrics = paint.fontMetricsInt
        val baseline = (drawBottom + drawTop - fontMetrics.bottom - fontMetrics.top) / 2.0f
        val start = drawLeft + ((drawRight - drawLeft) - paint.measureText(text)) / 2.0f
        canvas.drawText(text, start, baseline, paint)
    }

    fun drawTextOnRectCenter(
        canvas: Canvas,
        paint: Paint,
        text: String,
        rectF: RectF) {
        drawTextOnRectCenter(
            canvas,
            paint,
            text,
            rectF.left,
            rectF.top,
            rectF.right,
            rectF.bottom
        )
    }

    fun drawTextOnRectCenterRight(
        canvas: Canvas,
        paint: Paint,
        text: String,
        drawLeft: Float,
        drawTop: Float,
        drawRight: Float,
        drawBottom: Float) {
        val fontMetrics = paint.fontMetricsInt
        val baseline = (drawBottom + drawTop - fontMetrics.bottom - fontMetrics.top) / 2.0f
        val start = drawRight - paint.measureText(text)
        canvas.drawText(text, start, baseline, paint)
    }

    fun drawTextOnRectCenterRight(
        canvas: Canvas,
        paint: Paint,
        text: String,
        rectF: RectF) {
        drawTextOnRectCenterRight(
            canvas,
            paint,
            text,
            rectF.left,
            rectF.top,
            rectF.right,
            rectF.bottom
        )
    }

    fun drawTextOnRectCenterLeft(
        canvas: Canvas,
        paint: Paint,
        text: String,
        drawLeft: Float,
        drawTop: Float,
        drawRight: Float,
        drawBottom: Float) {
        val fontMetrics = paint.fontMetricsInt
        val baseline = (drawBottom + drawTop - fontMetrics.bottom - fontMetrics.top) / 2.0f
        val start = drawLeft
        canvas.drawText(text, start, baseline, paint)
    }

    fun drawTextOnRectCenterLeft(
        canvas: Canvas,
        paint: Paint,
        text: String,
        rectF: RectF) {
        drawTextOnRectCenterLeft(
            canvas,
            paint,
            text,
            rectF.left,
            rectF.top,
            rectF.right,
            rectF.bottom
        )
    }
}